package com.sg.web;

import com.github.pagehelper.PageInfo;
import com.sg.bean.Page;
import com.sg.bean.Role;
import com.sg.bean.User;
import com.sg.service.RoleService;
import com.sg.service.UserService;
import com.sg.vo.UsedVO;
import com.sg.vo.UserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zl
 * 用户控制器
 */
@Controller
public class UserController {

    Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @RequestMapping("/tologin")
    public String tologin(){
        logger.info("coming tologin...");
        return "login";
    }

    @RequestMapping("/login")
    public String login(String captcha, Model model, String uTel,String uPass, HttpSession session){
        logger.info("进入登录控制器");
        logger.info("验证码"+captcha);
        String str = session.getAttribute("simpleCaptcha").toString();
        if(captcha.equals(str)){
            UserVO userVO = userService.login(uTel,uPass);
            logger.info(userVO);
            if (userVO!=null){
                logger.info(1);
                session.setAttribute("createUser",userVO);
                return "redirect:main.action";
            }else{
                logger.info(2);
                //账号或密码输入错误
                model.addAttribute("mess","账号或密码输入错误");
                return "login";
            }
        }else {
            logger.info(3);
            //验证码输入错误
            model.addAttribute("mess","验证码输入错误");
            return "login";
        }
    }
    @RequestMapping("/toregist")
    public String toregist(){
        logger.info("去注册");
        return "user/regist";
    }

    @RequestMapping("/regist")
    public String regist(User user){
        boolean flag = userService.register(user);
        logger.info(flag);
        return "login";
    }

    @RequestMapping("/exit")
    public String exit(HttpSession session){
        logger.info("coming exit...");
        session.invalidate();
        return "redirect:/index.action";
    }

    @RequestMapping("admin/userlist")
    public String userlist(Model model,String pageNum,@RequestParam(defaultValue = "4") int pageSize){
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        List<UserVO> list = userService.findAllPage(Integer.parseInt(pageNum),pageSize);
        PageInfo pageInfo = new PageInfo(list);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        page.setServletUrl("/admin/userlist.action");
        model.addAttribute("page",page);
        return "admin/userlist";
    }

    /*修改用户*/
    @RequestMapping("admin/touserupdate")
    public String touserupdate(Model model,Integer uId){
        logger.info("进入修改页面");

        List<Role> list=roleService.findAll();
        User u = userService.findByUId(uId);
        model.addAttribute("rolelist",list);
        model.addAttribute("user",u);
       /* boolean b=userService.update(user);*/
        return "admin/userupdate";
    }
    @RequestMapping("admin/userupdate")
    public String userupdate(User user,Model model){
        logger.info(user);
        boolean b=userService.update(user);
       /* model.addAttribute("userlist",b);*/
        logger.info(b);
        return "redirect:userlist.action";
    }

    /*@RequestMapping("admin/touseradd")
    public String touseradd(Model model,User user){
        logger.info("进入增加页面");
        List<Role> list=roleService.findAll();
        model.addAttribute("rolelist",list);
        boolean b=userService.register(user);
        logger.info(b);
        return "admin/adduser";
    }
*/
    @RequestMapping("admin/userdelete")
    public String userdelete(int uId ){
        logger.info("进行删除");
        User user=userService.findByUId(uId);
        boolean b=userService.delete(user);
        logger.info(b);
        return "redirect:userlist.action";

    }









}
